//
//  ViewController.m
//  cellOpenAndClose
//
//  Created by fanxiao on 16/5/18.
//  Copyright © 2016年 11111. All rights reserved.
//

#import "ViewController.h"
#import "MyData.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *myTableView;
    NSMutableArray *dataArray;
}

@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 全局的数据源
    dataArray = [[NSMutableArray alloc] init];
    
    // 添加数据
    for (int i='A'; i<='H'; i++) {
        MyData *myData = [[MyData alloc] init];
        myData.name = [NSString stringWithFormat:@"%c",i];
        myData.array = [NSMutableArray array];
        for (int j=0; j<1; j++) {
            [myData.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];
        }
        [dataArray addObject:myData];
    }
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.rowHeight = 30;
    [self.view addSubview:myTableView];
}

// 组数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [dataArray count];
}
// 根据状态来判断是否显示该组，隐藏组把组的行数设置为0即可
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MyData *data = [dataArray objectAtIndex:section];
    if (data.isShow) {
        return  data.array.count;
    }else{
        return  0;
    }
}
// 添加每行显示的内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName ];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    MyData *data = [dataArray objectAtIndex:indexPath.section];
    NSString *str = [[data array] objectAtIndex:indexPath.row];
    cell.textLabel.text = str;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
// 定义头标题的视图，添加点击事件
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyData *data = [dataArray objectAtIndex:section];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 30);
    [btn setTitle:data.name forState:UIControlStateNormal];
    btn.tag = section;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    if (section%2) {
        btn.backgroundColor = [UIColor darkGrayColor];
    }else{
        btn.backgroundColor = [UIColor lightGrayColor];
    }
    return btn;
}
- (void) btnClick:(UIButton *)btn
{
    MyData *data = [dataArray objectAtIndex:btn.tag];
    
    // 改变组的显示状态
    if (data.isShow) {
        [data setIsShow:NO];
    }else{
        [data setIsShow:YES];
    }
    // 刷新点击的组标题，动画使用卡片
    NSInteger lastSelected = [self searchLastOpenSectionWithArr:dataArray currentSection:btn.tag];
    if (lastSelected != -1) {
        [myTableView beginUpdates];
        [myTableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];
        [myTableView reloadSections:[NSIndexSet indexSetWithIndex:lastSelected] withRowAnimation:UITableViewRowAnimationFade];
        [myTableView endUpdates];
    }else{
        [myTableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//查找上次展开的cell
-(NSInteger)searchLastOpenSectionWithArr:(NSArray*)arr  currentSection:(NSInteger)tag
{
    for (int i = 0; i < arr.count; i++) {
        MyData *data = [dataArray objectAtIndex:i];
        if (data.isShow && i != tag) {
            [data setIsShow:NO];
            return i;
        }
    }
    return -1;
}

@end
